import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('../views/Login')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/Home'),
    redirect: '/walcome',
    children: [
      {
        path: '/users',
        name: 'users',
        component: () => import('../views/Users')
      },
      {
        path: '/walcome',
        name: 'walcome',
        component: () => import('../views/Users/walcome')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../views/Roles')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../views/Roles/rights')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/Goods')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../views/Goods/categories')
      },
      {
        path: '/add',
        name: 'add',
        component: () => import('../views/Goods/add')
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import('../views/Reports')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/Orders')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('../views/Goods/params')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// router.beforeEach((to, from, next) => {
//   const flag = to.matched.some((item) => {
//     return item.meta.status
//   })
//   if (flag) {
//     const token = sessionStorage.getItem('token')
//     if (!token) {
//       next('/login')
//     }
//   }
//   next()
// })
// router.beforeEach((to, from, next) => {
//   if (to.path === '/login') {
//     next()
//   } else {
//     if (sessionStorage.getItem('token')) {
//       next()
//     } else {
//       next('/login')
//     }
//   }
// })

// router.beforeEach((to, from, next) => {
//   if (to.path === '/login') {
//     next()
//   } else {
//     if (sessionStorage.getItem('token')) {
//       next()
//     } else {
//       next('/login')
//     }
//   }
// })

// router.beforeEach((to, from, next) => {
//   if (to.path === '/login') {
//     next()
//   } else {
//     if (sessionStorage.getItem('token')) {
//       next()
//     } else {
//       next('/login')
//     }
//   }
// })

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    if (sessionStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
